import { EChartOption } from 'echarts-ng2/src';
import { Curver } from 'app/classes/BackData';

export class Device {
  constructor(
    public name: string,
    public meterID: string,
    ) {
  }
}
export class Select {
  constructor(
      public name: string,
      public value: string
    ) {
  }
}
export class Nav {
  constructor(
    public name: string,
    public url: string,
    public type: string
  ) {

  }
}


export class ChartsQueryCondition {
  constructor(
    public meterID: string,
    public datatype: string,
    public begindate: Date,
    public enddate: Date,
    public time: string
  ) {

  }
}

export class ChartsQueryConditionStr {
  constructor(
    public meterID: string,
    public datatype: string,
    public begindate: string,
    public enddate: string,
    public datetype?: string,
    public time?: string
  ) {

  }
}

export class FormatDate {
  constructor(
    public date: Date,
  ) {
  }
  public formatToString(backDays?): string {
    if (backDays) {
      this.date.setDate(this.date.getDate() - backDays);
      const _date =
      `${this.date.getFullYear()}-${this.PrefixInteger(this.date.getMonth() + 1, 2)}-${this.PrefixInteger(this.date.getDate(), 2)}`;
      return _date;
    } else {
      const _date =
      `${this.date.getFullYear()}-${this.PrefixInteger(this.date.getMonth() + 1, 2)}-${this.PrefixInteger(this.date.getDate(), 2)}`;
      return _date;
    }
  }
  public formatToObj(backDays?): object {
    if (backDays) {
      this.date.setDate(this.date.getDate() - backDays);
      const _date = {
        year: this.date.getFullYear(),
        month: this.date.getMonth() + 1,
        day: this.date.getDate()
      }
      return _date
    } else {
      const _date = {
        year: this.date.getFullYear(),
        month: this.date.getMonth() + 1,
        day: this.date.getDate()
      }
      return _date
    }
  }
  public getTime(): string {
    let time = '';
    time = `${this.PrefixInteger(this.date.getHours(), 2)}:${this.PrefixInteger(this.date.getMinutes(), 2)}`
    return time
  }
  private PrefixInteger(num, n) {
    return (Array(n).join('0') + num).slice(-n);
  }
}

export class DataChartParams {
  constructor (
    public meterID: string,
    public begindate
  ) {

  }
}
export class DataFilter {
  constructor(
    public data: Array< object >
  ) {
  }
  public voltageFilter() {
    const _data = {
      va: [],
      vb: [],
      vc: []
    };

    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.va.push([atTime, e['va'] ]);
      _data.vb.push([atTime, e['vb'] ]);
      _data.vc.push([atTime, e['vc'] ]);
    });
    return _data;
  }

  public currentFilter() {
    const _data = {
      ia: [],
      ib: [],
      ic: []
    };
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.ia.push([atTime, e['ia'] ]);
      _data.ib.push([atTime, e['ib'] ]);
      _data.ic.push([atTime, e['ic'] ]);
    });
    return _data;
  }

  public aqaccvalueFilter() {
    const _data = []
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.push([atTime, e['aqaccvalue'] ]);
    });
    return _data;
  }
  // 功率因素
  public powerfactorFilter() {
    const _data = []
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.push([atTime, e['powerfactoro'] ]);
    });
    return _data;
  }
  // 有功功率
  public activepowerFilter() {
    const _data = []
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.push([atTime, e['activepowero'] ]);
    });
    return _data;
  }
  // 无功功率
  public reactivepowerFilter() {
    const _data = []
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.push([atTime, e['reactivepowero'] ]);
    });
    return _data;
  }
  // 电流电压不平衡
  public imbalanceFilter() {
    const _data = {
      vu: [],
      iu: []
    };
    this.data.forEach(e => {
      const atTime = parseInt(e['recordtime'], 10);
      _data.vu.push([atTime, e['vu'] ]);
      _data.iu.push([atTime, e['iu'] ]);
    });
    return _data;
  }
}


export class ChartOption extends DataFilter {
  public option: EChartOption = {};
  constructor (
    public data: Array<object>,
    public datatype: string,
    public _now?: number,
    public now_?: number
  ) {
    super(data);
  }

  public getOption() {
    switch (this.datatype) {
    // 电压曲线
      case '1001': {
        const v_data = this.voltageFilter();
        return this.option = {
          title: {
            text: '电压曲线'
          },
          tooltip: {},
          legend: {
            data: ['VA', 'VB', 'VC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'VA',
              type: 'line',
              data: v_data.va
            },
            {
              name: 'VB',
              type: 'line',
              data: v_data.vb
            },
            {
              name: 'VC',
              type: 'line',
              data: v_data.vc
            }
          ]
        };
      }
    // 电流曲线
      case '1002': {
        const i_data = this.currentFilter();
        console.log(i_data);
        return this.option = {
          title: {
            text: '电流曲线'
          },
          tooltip: {},
          legend: {
            data: ['IA', 'IB', 'IC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'IA',
              type: 'line',
              data: i_data.ia
            },
            {
              name: 'IB',
              type: 'line',
              data: i_data.ib
            },
            {
              name: 'IC',
              type: 'line',
              data: i_data.ic
            }
          ]
        };
      }
    // 电量曲线
      case '1003': {
        const data = this.aqaccvalueFilter();
        return this.option = {
          title: {
            text: '电量曲线'
          },
          tooltip: {},
          legend: {
            data: ['电量']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: '电量',
              type: 'line',
              data: data,
              markPoint: {
                data: [
                    {type: 'max', name: '最大值'},
                    {type: 'min', name: '最小值'}
                ]
              },
              areaStyle: {
                normal: {
                }
              }
            }
          ]
        };
      }
    // 功率因素
      case '1004': {
        const powerfactor = this.powerfactorFilter();
        return this.option = {
          title: {
            text: '功率因素'
          },
          tooltip: {},
          legend: {
            data: ['功率因素']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {
            min: 0,
            max: 1,
            interval: 0.25,
          },
          series: [
            {
              name: '功率因素',
              type: 'line',
              data: powerfactor
            }
          ]
        };
      }
    // 有功功率
      case '1005': {
        const activepower = this.activepowerFilter();
        return this.option = {
          title: {
            text: '有功功率Kw'
          },
          tooltip: {},
          legend: {
            data: ['有功功率']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {
            min: 0,
            max: 200
          },
          series: [
            {
              name: '有功功率',
              type: 'line',
              data: activepower
            }
          ]
        };
      }
    // 无功功率
      case '1006': {
        const reactivepower = this.reactivepowerFilter();
        return this.option = {
          title: {
            text: '无功功率kw'
          },
          tooltip: {},
          legend: {
            data: ['无功功率']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: '无功功率',
              type: 'line',
              data: reactivepower
            }
          ]
        };
      }
      // 电流电压不平衡
      case '1007': {
        const u_data = this.imbalanceFilter();
        return this.option = {
          title: {
            text: '电流电压不平衡'
          },
          tooltip: {},
          legend: {
            data: ['VU', 'IU']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: [{
              name: '电压不平衡',
              type: 'value',
              min: 0,
              max: 1,
              interval: 0.25,
            },
            {
              name: '电流不平衡',
              min: 0,
              max: 1,
              type: 'value',
              interval: 0.25,
          }],
          series: [
            {
              name: 'VU',
              type: 'line',
              data: u_data.vu
            },
            {
              name: 'IU',
              yAxisIndex: 1,
              type: 'line',
              data: u_data.iu
            }
          ]
        };
      }
      // 谐波电流
      case '1008': {
        const i_data = this.currentFilter();
        return this.option = {
          title: {
            text: '谐波电流'
          },
          tooltip: {},
          legend: {
            data: ['IA', 'IB', 'IC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'IA',
              type: 'line',
              data: i_data.ia
            },
            {
              name: 'IB',
              type: 'line',
              data: i_data.ib
            },
            {
              name: 'IC',
              type: 'line',
              data: i_data.ic
            }
          ]
        };
      }
      // 谐波电压
      case '1009': {
        const v_data = this.voltageFilter();
        return this.option = {
          title: {
            text: '谐波电压'
          },
          tooltip: {},
          legend: {
            data: ['VA', 'VB', 'VC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'VA',
              type: 'line',
              data: v_data.va
            },
            {
              name: 'VB',
              type: 'line',
              data: v_data.vb
            },
            {
              name: 'VC',
              type: 'line',
              data: v_data.vc
            }
          ]
        };
      }
      // 谐波电流含有率
      case '1010': {
        const i_data = this.currentFilter();
        return this.option = {
          title: {
            text: '谐波电流'
          },
          tooltip: {},
          legend: {
            data: ['IA', 'IB', 'IC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'IA',
              type: 'line',
              data: i_data.ia
            },
            {
              name: 'IB',
              type: 'line',
              data: i_data.ib
            },
            {
              name: 'IC',
              type: 'line',
              data: i_data.ic
            }
          ]
        };
      }
      // 谐波电压含有率
      case '1011': {
        const v_data = this.voltageFilter();
        return this.option = {
          title: {
            text: '谐波电压含有率'
          },
          tooltip: {},
          legend: {
            data: ['VA', 'VB', 'VC']
          },
          xAxis: {
            splitLine: {show: false},
            min: this._now,
            max: this.now_,
            type: 'time',
          },
          yAxis: {},
          series: [
            {
              name: 'VA',
              type: 'line',
              data: v_data.va
            },
            {
              name: 'VB',
              type: 'line',
              data: v_data.vb
            },
            {
              name: 'VC',
              type: 'line',
              data: v_data.vc
            }
          ]
        };
      }
      default: return this.option;
    }
  }
}
